import * as Common from './common.js'
import { Util } from "./util.js";
import { MsgMgr } from './msgMgr.js'
import { Bag } from './bag/bag.js';
export class HomePage
{
    static goodsList = [];
    static checkedInfoBase = true;
    static url = Common.UrlBase + Common.ApiEnums.UserMgr;
    constructor()
    {
        // 构造函数  
    }
    static btnFunc(str)
    {
        var user_id = $.cookie('user_id');

        if (str == 'bagBtn')
        {
            Bag.updateBag();

        } else if (str == "infoBtn")
        {

            $("#mymodal").modal("show");
        } else if (str == "msgBtn")
        {
            if ((parseInt(user_id) != 1000 && parseInt(user_id) != 1) && Util.getCurrentMsgType() == 3)
                alert("系统消息不能发言")
            else
                $("#msgmodal").modal("show");

        } else if (str == "renameBtn")
        {

            $("#renamemodal").modal("show");

        }
    }

    static btnBind()
    {
        //事件绑定
        $("#msgBtn").on('click', function ()
        {
            HomePage.btnFunc('msgBtn');
        });
        $("#infoBtn").on('click', function ()
        {
            HomePage.btnFunc('infoBtn');
        });
        $("#renameBtn").on('click', function ()
        {
            HomePage.btnFunc('renameBtn');
        });
        $("#bagBtn").on('click', function ()
        {
            HomePage.btnFunc('bagBtn');
        });
    }
    static onReady()
    {
        HomePage.btnBind();
        if ($.cookie("user_id") == null)
        {
            alert("请登录!");
            window.location.href = "index.html";
            return;
        }

        var user_id = $.cookie('user_id');
        $.post(
            HomePage.url,
            {
                "action": "getUserInfoAll",
                "user_id": user_id
            },
            function (res)
            {

                const resobj = JSON.parse(res);
                console.log(resobj.data[0])
                var ele = (resobj.data)[0];
                $.cookie("user", JSON.stringify(ele), { expires: 7 });
                //console.log(ele)
                var infoBaseStr = ["<h2 id='vtype'>" + ele.level + "级" + Util.getJobNameByJobId(ele.job) + "</h2>",
                "<h2>id:" + (1000 + parseInt(ele.id)) + "</h2>",
                "<h2>名字:" + ele.name + "</h2>",
                "<h2>性别：" + (parseInt(ele.gender) == 0 ? "男" : "女") + "</h2>",
                "<h2>国家：" + (parseInt(ele.contry) == 0 ? "魏" : (parseInt(ele.contry) == 1 ? "蜀" : "吴")) + "</h2>",
                "<h2>居住地：" + (parseInt(ele.contry) == 0 ? "洛阳" : (parseInt(ele.contry) == 1 ? "成都" : "建业")) + "</h2>",
                "<h2>当前体力值:" + (ele.phystr) + "</h2>"].join("\n");
                var tmp = ["<h2>生命：" + (ele.hp) + "/" + ele.hpmax + "</h2>",
                "<h2>精力：" + (ele.mp) + "/" + ele.mpmax + "</h2>",
                "<h2>攻击：" + (ele.attack) + "</h2>",
                "<h2>防御：" + (ele.defence) + "</h2>",
                "<h2>速度：" + (ele.speed) + "</h2>",
                "<h2>金币：" + (ele.coin) + "</h2>",
                "<h2>银两：" + (ele.tael) + "</h2>",
                    "<div class='form-check'>",
                "<input class='form-check-input' type='checkbox' onclick='checkbox(this)' value='' id='flexCheckDefault' checked='" + HomePage.checkedInfoBase + "'>",
                    "<label class='form-check-label' for='flexCheckDefault'>",
                    "个人信息常驻",
                    "</label></div>"
                ].join("\n");
                var node = $(tmp);
                var nodeInfoBase = $(infoBaseStr);
                $("#info").after(node);
                $("#infoBase").append(nodeInfoBase);
                $('#puser')[0].innerHTML = "欢迎:" + Util.getKvFromCookie("name");

            }
        );


        var valInput = $("#nameInput");
        document.getElementById("renamemodal").addEventListener('hide.bs.modal', event =>
        {
            if (valInput.val().length > 0 && valInput.val().length < 5)
            {
                //regist
                var user_id = $.cookie('user_id');
                var content = valInput.val();
                var type = 3;
                var oldname = Util.getKvFromCookie("name");
                netUpdateUserByKv(user_id, "name", content, 1, function ()
                {
                    //update local
                    // setKvFromCookieBag("15", Util.getKvFromCookieBag("15") -1);
                    setKvFromCookieUser("name", content);
                    //updateBagView();
                    $('#puser')[0].innerHTML = "欢迎:" + Util.getKvFromCookie("name");

                });
                MsgMgr.sendMsg("1000", "系统", "玩家(" + oldname + ")说,我改名了,以后就叫我(" + content + ")吧", 3, () =>
                {
                    MsgMgr.getMsgByType(3);
                });
            } else
            {
                alert("格式输入错误")
            }
        })
        var msgInput = $("#msgInput");
        document.getElementById("msgmodal").addEventListener('hide.bs.modal', event =>
        {
            if (msgInput.val().length > 0 && msgInput.val().length < 20)
            {
                //regist
                var user_id = $.cookie('user_id');
                var content = msgInput.val();
                var type = Util.getCurrentMsgType();
                MsgMgr.sendMsg(user_id, Util.getKvFromCookie("name"), content, type, () =>
                {
                    MsgMgr.getMsgByType(type);
                });
            } else
            {
                alert("格式输入错误")
            }
        })
        MsgMgr.getMsgByType(Util.getCurrentMsgType());
        const triggerTabList = document.querySelectorAll('#myTab button')
        triggerTabList.forEach(triggerEl =>
        {
            triggerEl.addEventListener('click', function ()
            {
                MsgMgr.getMsgByType(Util.getCurrentMsgType());

            })
        })
    }
}

$(document).ready(function ()
{
    HomePage.onReady();
});


function checkbox(obj)
{
    HomePage.checkedInfoBase = obj.checked;
    var colNode = document.getElementById("collapseExample");
    if (obj.checked)
        colNode.classList.add("show");
    else
        colNode.classList.remove("show");


}


function netUpdateUserByKv(user_id, key, val, val_type, cb)
{
    $.post(
        HomePage.url,
        {
            "action": "updateUserKvByid",
            "user_id": user_id,
            "key": key,
            "val": val,
            "val_type": val_type
        }, function (res)
    {
        if (cb)
        {
            cb();

        }
    }
    );
}

function netUseGoods(gid, counts)
{

    var user_id = $.cookie('user_id');
    $.post(
        HomePage.url,
        {
            "action": "addUserGoods",
            "user_id": user_id,
            "goods_id": gid,
            "counts": counts
        },
        function (res)
        {


        }
    );
}
